<?php 
namespace App\Http\Controllers\Admin;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Crypt;
require_once '/app/Http/Controllers/org/code.php';
class LoginController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function login()
	{
		
		if(!empty($_POST)){
			$post = $_POST;
			//验证验证码
			if($post['code'] != $_SESSION['code']){
				return redirect('admin/login')->with('name', '验证码错误');
			}
			//验证用户和密码
			$data = DB::table('admin')->where('name',$post['name'])->first();

			if($data->name == $post['name'] && $data->password == md5($post['password']) ){
				session(['admin_user'=>$data]);
				return redirect('admin/index');
			}else{
				return redirect('admin/login')->with('name', '用户名或密码错误');
			}
		}else{
			return view('admin.login'); 
		}
		
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function code()
	{
		
		$code= new \Code(1,0,50,100);
		$_SESSION['code']= $code->getCode();
		$code->outImage();
	}

	//密码加密
	public function crypt(){
		// $str = '123456';
		$encrypted = Crypt::encrypt('Hello world.');//加密
		echo $encrypted ,'<br/>';
		//$decrypted = Crypt::decrypt($encrypted);//解密
		
	}
}
